<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-button type="primary" @click="addClick" style="margin-bottom: 24px">新增</a-button>
        <a-table size="default" :rowKey="record => record.id" :columns="columns" :dataSource="dataSource" :pagination="paginationPropsCase" @change="handleCaseTableChange" :scroll="{ x: 1200 }">
          <span slot="action" slot-scope="text, record">
            <a @click="handleAddAndEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定禁用吗?" ok-text="Yes" cancel-text="No" @confirm="cancel(record.id, '1')" v-if="record.isDel == 0">
              <a href="#">禁用</a>
            </a-popconfirm>
            <a-popconfirm title="确定启用吗?" ok-text="Yes" cancel-text="No" @confirm="cancel(record.id, '0')" v-if="record.isDel == 1">
              <a href="#">启用</a>
            </a-popconfirm>
          </span>
        </a-table>
      </div>
      <DeatilModel ref="deatilModel" @restOk="restOk"></DeatilModel>
    </a-card>
  </div>
</template>
<script>
import DeatilModel from './deatilModel'
import { queryGoods, modifyPost } from '@/api/box/product'
import { STable } from '@/views/box/components'
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'

export default {
  // mixins: [listMixin],
  components: {
    DeatilModel
    // STable
  },
  data() {
    return {
      // columns,
      columns: [
        { title: '商品款号', align: 'center', dataIndex: 'productCode' },
        {
          title: '营销标签',
          align: 'center',
          dataIndex: 'tagDictCode',
          customRender: (text, record, index) => {
            //字典值替换通用方法
            return filterDictText(this.advertLoc, text)
          }
        },
        { title: '添加时间', align: 'center', dataIndex: 'updateTime' },
        { title: '置顶时间', align: 'center', customRender: (text, record) => `${record.beginTime}～${record.endTime}` },
        { title: '添加人', dataIndex: 'createBy', align: 'center' },
        { title: '操作', fixed: 'right', width: 120, align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      paginationPropsCase: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
        showTotal: total => `共有 ${total} 条数据`
      },
      advertLoc: [],
      dataSource: [],
      queryParam: {
        categoryId: '',
        fashionerType: '',
        status: ''
      }
    }
  },
  mounted() {
    this.initDictConfig()
  },
  created() {
    this.loadData()
  },
  methods: {
    handleCaseTableChange(pagination) {
      this.paginationPropsCase.pageNo = pagination.current
      this.loadData()
    },
    addClick() {
      this.$refs.deatilModel.show('add')
    },
    handleAddAndEdit(item) {
      this.$refs.deatilModel.show(item)
    },
    loadData() {
      const { pageNo, pageSize } = this.paginationPropsCase
      const data = {
        requestData: {
          bizType: 'box_product_dict'
        },
        page: {
          pageNo: pageNo,
          pageSize: pageSize
        }
      }

      return queryGoods(data).then(res => {
        if (res.code === 0) {
          // this.initDictConfig()

          this.dataSource = res.data
          this.paginationPropsCase.total = res.page.count
        } else {
          this.$message.error(res.msg)
          return {
            data: [],
            page: {
              pageNo: 0,
              count: 0,
              pageSize: 10
            }
          }
        }
      })
    },
    cancel(id, type) {
      let data = {
        requestData: {
          id: id,
          isDel: type,
          bizType: 'box_product_dict'
        }
      }
      modifyPost(data).then(res => {
        if (res.code == 0) {
          this.loadData()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    initDictConfig() {
      //初始化字典 -box业务商品标签
      initDictOptions('box_product_dict').then(res => {
        if (res.success) {
          this.advertLoc = res.result
        }
      })
    },

    restOk() {
      this.loadData()
    }
  }
}
</script>

